﻿using Ecommerce.Domain.Dto;
using Ecommerce.Repository;
using EcommerceRabbit.Repository.Impl;
using EcommerceRabbit.Repository.Interface;
using EcommerceRabbit.Servies.traceability;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace Ecommerce.API.Extensions
{
    public static class ServicesExtension
    {
        public static IServiceCollection Addcontext<T>(this IServiceCollection services,string? connectionstring,bool enablesqlLog) where T : DbContext
        {
            return services.AddDbContext<EFContexts>(w =>
            {
                w.UseSqlServer(connectionstring, a =>
                {
                    a.EnableRetryOnFailure(
                        maxRetryCount:10,
                        maxRetryDelay:TimeSpan.FromSeconds(60),
                        errorNumbersToAdd:null
                        );
                }).EnableSensitiveDataLogging();
                if(enablesqlLog)
                {
                    w.UseLoggerFactory(LoggerFactory.Create(configure =>
                    {
                        configure.AddFilter((category,level)=>category==DbLoggerCategory.Database.Command.Name && level==LogLevel.Information).AddConsole();
                    }));
                }
            });
        }

        public static IServiceCollection AppServices(this IServiceCollection services)
        {
           services.AddScoped<ITraceServices, TraceServices>();
           services.AddScoped<ITraceRepository,TraceRepository>();
           services.AddScoped<IImageRepository,ImageRepository>();
           services.AddScoped<IClutivaReposiotry,ClutivaReposiotry>();
            services.AddScoped<IImmuneRepository, ImmuneRepository>();
            services.AddScoped<IEnterpriseRepository, EnterpriseRepository>();
            return services;
        }
    }
}
